#!/bin/bash

#=======================================================================
# git-realclean
# File ID: a0e45088-d9f3-11e1-b9d0-00c0a8deee11
# Compress a Git repo really good
# License: GNU General Public License version 2 or later.
#=======================================================================

if [ -d .git/. ]; then
    dudir=.git
else
    dudir=.
fi

test -d $dudir/objects/. || { echo git-realclean: .git/objects/ or objects/ not found >&2; exit 1; }
tot_before=$((find $dudir/objects -type f -printf '%s+' ; echo 0) | bc)
echo tot_before: $(echo $tot_before | commify)
echo ==== git reflog expire --expire=now --expire-unreachable=now --all
git reflog expire --expire=now --expire-unreachable=now --all
echo ==== git gc --prune=now "$@"
git gc --prune=now "$@"
tot_after=$((find $dudir/objects -type f -printf '%s+' ; echo 0) | bc)
saved=$(echo "$tot_before-$tot_after" | bc)
echo -----------
echo tot_before: $(echo $tot_before | commify)
echo tot_after: $(echo $tot_after | commify)
[ "$tot_before" != "0" ] && echo "Totally saved: $(echo $saved | commify) ($(echo "scale=4; 100*$saved/$tot_before" | bc -l)%)"
